package basic.chapter5;

/**
 * @author Kevin
 * @date 2020/2/21 13:34
 * 细粒度锁实现账户转账，可能死锁
 */
public class Account {
    private int balance;
    // 转账
    void transfer(Account target, int amt) {
        //锁定转出账户
        synchronized(this) {
            //锁定转入账户
            synchronized (target) {
                if (this.balance > amt) {
                    this.balance -= amt;
                    target.balance += amt;
                }
            }
        }
    }
}
